package com.iflytek.xfMscMain;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.GrammarListener;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.util.ResourceUtil;
import com.iflytek.cloud.util.ResourceUtil.RESOURCE_TYPE;
import com.iflytek.speech.util.FucUtil;
import com.iflytek.speech.util.JsonParser;
import com.iflytek.speech.util.XmlParser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class AsrFileUseDemo extends Activity implements OnClickListener {
    private TextView isrText;
    private Button fileRecognizeBT;
    private XfMain xfMain;
    private SpeechRecognizer mAsr;
    private String mResultType = "json";
    private String mEngineType = SpeechConstant.TYPE_LOCAL;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.myisrdemo);
        initLayout();
        xfMain = XfMain.getInstance();
        mAsr = xfMain.getAsr(this);
    }


    /**
     * 初始化Layout。
     */
    private void initLayout() {
        findViewById(R.id.isr_stop).setOnClickListener(this);
        findViewById(R.id.isr_cancel).setOnClickListener(this);
        fileRecognizeBT = findViewById(R.id.fileRecognizeBT);
        fileRecognizeBT.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.isr_cancel) {
            mAsr.stopListening();
        } else if (id == R.id.fileRecognizeBT) {
            mAsr.startListening(new RecognizerListener() {
                @Override
                public void onVolumeChanged(int volume, byte[] data) {
//                    showTip("当前正在说话，音量大小：" + volume);
                    Log.d("buggg", "返回音频数据：" + data.length);
                }

                @Override
                public void onResult(final RecognizerResult result, boolean isLast) {
                    if (null != result && !TextUtils.isEmpty(result.getResultString())) {
                        Log.d("", "rtecognizer resul：" + result.getResultString());
                        String text = "";
                        if (mResultType.equals("json")) {
                            text = JsonParser.parseGrammarResult(result.getResultString(), mEngineType);
                        } else if (mResultType.equals("xml")) {
                            text = XmlParser.parseNluResult(result.getResultString());
                        } else {
                            text = result.getResultString();
                        }
                        // 最终显示
                        isrText.setText(text);
                    } else {
                        Log.d("buggg", "recognizer result : null");
                    }
                }

                @Override
                public void onEndOfSpeech() {
                    // 此回调表示：检测到了语音的尾端点，已经进入识别过程，不再接受语音输入
//                    showTip("结束");
                }

                @Override
                public void onBeginOfSpeech() {
                    // 此回调表示：sdk内部录音机已经准备好了，用户可以开始语音输入
//                    showTip("开始");
                }

                @Override
                public void onError(SpeechError error) {
                    isrText.setText("错误代码" + error.getErrorCode());
//                    showTip("onError Code：" + error.getErrorCode());
                }

                @Override
                public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
                    // 以下代码用于获取与云端的会话id，当业务出错时将会话id提供给技术支持人员，可用于查询会话日志，定位出错原因
                    // 若使用本地能力，会话id为null
                    //	if (SpeechEvent.EVENT_SESSION_ID == eventType) {
                    //		String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
                    //		Log.d(TAG, "session id =" + sid);
                    //	}
                }
            });
        }
    }


}
